#include <iostream>
#include <cstring>

using namespace std;

const int N = 310;
int a[N], f[N], dp[N][N];
int n;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
		f[i] = f[i - 1] + a[i];
	}
	memset(dp, 0x3f, sizeof dp);
	for(int i = 1; i <= n; i++) dp[i][i] = 0;
	for(int len = 2; len <= n; len++)
	{
		for(int i = 1; i + len - 1 <= n; i++)
		{
			int j = i + len - 1;
			for(int k = i; k < j; k++)
			{
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + f[j] - f[i - 1]);
			}
		}
	}
	cout << dp[1][n] << endl;
	return 0;
}
